package com.dts.evaluate.callback;

import com.dts.evaluate.utils.LogUtil;
import com.google.gson.Gson;
import com.lzy.okhttputils.callback.AbsCallback;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import okhttp3.Response;

/**
 * Created by user on 2016/8/1.
 */
public abstract class BeanCallBack<T> extends AbsCallback<T> {
    @Override
    public T parseNetworkResponse(Response response) throws Exception {
        Type type = this.getClass().getGenericSuperclass();
        if (type instanceof ParameterizedType) {
            //如果用户写了泛型，就会进入这里，否者不会执行
            ParameterizedType parameterizedType = (ParameterizedType) type;
            Type beanType = parameterizedType.getActualTypeArguments()[0];
            if (beanType == String.class) {
                //如果是String类型，直接返回字符串
                return (T) response.body().string();
            } else {
                //如果是 Bean List Map ，则解析完后返回
                String s = response.body().string();

                s = s.replace("/n","");
                return new Gson().fromJson(s, beanType);
            }
        } else {
            //如果没有写泛型，直接返回Response对象
            return (T) response;
        }
    }

}
